package com.qfedu.springmvc02.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.springmvc02.entity.User;
import com.qfedu.springmvc02.response.Response;
import com.qfedu.springmvc02.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vue")
public class UserVueController {

    @Autowired
    UserService userService;
    @RequestMapping("showAll")
    public Response<PageInfo<User>> showAll(@RequestParam(value = "pageIndex",required = false,defaultValue = "1" ) Integer pageIndex,
                                  @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
        System.out.println(pageIndex);
        System.out.println(pageSize);
        PageHelper.startPage(pageIndex,pageSize);
        List<User> users = userService.selectAll();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return Response.success(pageInfo);
    }
    @DeleteMapping("/delete/{id}")
    public Response<User> delete(@PathVariable("id" )Integer id){
        userService.delete(id);
        return Response.success();
    }

    @GetMapping("/user/{id}")
    public Response<User> selectById(@PathVariable("id")Integer id){
        User user = userService.selectOne(id);
        return Response.success(user);
    }

    @PostMapping("/user")
    public Response<User> update(User user){
        System.out.println(user);
        userService.update(user);
        return Response.success();
    }
}
